package com.yuexun.dhdoctor.base;

import android.content.pm.PackageManager;

import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.main.bean.UpdateBean;
import com.yuexun.dhdoctor.module.main.model.impl.UpdateModel;
import com.yuexun.dhdoctor.module.main.presenter.IUpdatePresenter;
import com.yuexun.dhdoctor.module.main.presenter.impl.UpdatePresenter;
import com.yuexun.dhdoctor.module.main.ui.view.IUpdateView;
import com.yuexun.dhdoctor.module.my.ui.Act_StartPassword;
import com.yuexun.dhdoctor.util.UpdateManager;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.utils.PreferenceHelper;

/**
 * Created by work-jx on 2016/8/4.
 */
public class BaseKJActivity extends KJActivity implements IUpdateView {
    IUpdatePresenter presenter;

    private UpdateManager updateManager;

    @Override
    public void setRootView() {
        ExitApplication.getInstance().addActivity(this);
        }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (((AppContext) getApplication()).getIsLocked()) {
            ((AppContext) getApplication()).setIsLocked(false);
            Boolean ispassword = PreferenceHelper.readBoolean(this, AppConfig.USER_INFO, AppConfig.ISPASSWORD);
            if (ispassword) {
                showActivity(this, Act_StartPassword.class);
            }
        }
        presenter = new UpdatePresenter(new UpdateModel(),this);
        presenter.update();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (this instanceof Act_Main) {
//            ((AppContext) getApplication()).setIsLocked(true);
//        }
    }

    @Override
    public void showUpdateInfo(UpdateBean updateBean) {
        try {
            int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
            if (updateBean.getVersionCode() > versionCode) {
                if (updateManager == null) {
                    updateManager = new UpdateManager(this, updateBean, versionCode);
                    updateManager.showNoticeDialog();
                } else {
                    if (!updateManager.isShowDialog()) {
                        updateManager.showNoticeDialog();
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showUpdateError() {

    }
}
